<?php
namespace app\face\controller;
use think\Controller; 
use think\Db;
use think\Log;
class Applet extends Controller
{
	public function getuserinfo()
	{
		$user_id=request()->post("user_id");
		$userinfo=Db::table("face_user_room")->where("user_id",$user_id)->find();
		return json_encode($userinfo);
	}

	public function userinquire()
	{
		$user_id=request()->post("user_id");
		$userinfo=Db::table("face_user_room")
					->where("user_id",$user_id)
					->find();
		if(empty($userinfo))
		{
			return json(["bindstatus"=>"0"]);//未绑定
		}else{
			return json(["bindstatus"=>"1"]);//已绑定
		}
	}
	//业主获取短信验证码
	public function verifyroom()
	{
		$data=request()->post();
		//查询该用户是否绑定房屋

		//查询该手机号下是否存在房屋
		$roominfo=Db::table("roominfo")->where("owner_phone",$data["phone"])->find();
		if(empty($roominfo))
		{
			return json(["errCode"=>"1",'error' => '房屋信息不存在']);
		}
        //查询业主是否已经绑定该房屋
        $userroom=Db::table("face_user_room")
        			->where("room_id",$roominfo["id"])
        			->find();
        if(!empty($userroom))
        {
        	return json(["errCode"=>"1",'error' => '该房屋已经绑定']);
        }
        //$arr=array("errCode"=>"0","msg"=>"验证码发送成功");
        //return json_encode($arr);
        $result=$this->sendmsg($data["phone"]);
        return $result;
	}
	//业主绑定房屋
	public function bindroom()
	{
		$data=request()->post();
		//手机号验证
		$smsverify=Db::table("face_sms_verify")
				->where("phone",$data["phone"])
				->where("code",$data["code"])
				->where("ctime",'>',(time()-80))
				->order("id desc")
				->find();
		if(empty($smsverify))
		{
            return json(["errCode"=>"1",'error' => '验证码错误']);
		}else
		{
			$info["status"]="2";
			Db::table("face_sms_verify")
					->where("id",$smsverify["id"])
					->update($info);
			//执行绑定房屋操作
			$roominfo=Db::table("roominfo")
						->where("owner_phone",$data["phone"])
						->find();
			Log::write($roominfo,"房屋信息");
			if(empty($roominfo))
			{
				return json(["errCode"=>"1",'error' => '房屋绑定失败，请重新获取验证码绑定1']);
			}
			$userroom=Db::table("face_user_room")
							->where("user_id",$data["user_id"])
							->find();
			if(!empty($userroom))
			{
				return json(["errCode"=>"1",'error' => '房屋绑定失败，请重新获取验证码绑定2']);
			}
			unset($data["code"]);
			$data["community_id"]=$roominfo["community_id"];
			$data["group"]=$roominfo["group"];
			$data["building"]=$roominfo["building"];
			$data["unit"]=$roominfo["unit"];
			$data["room"]=$roominfo["room"];
			$data["room_id"]=$roominfo["id"];
			$data["room_address"]=$roominfo["address"];
			$data["create_time"]=time();
			$communityinfo=Db::table("community")
								->where("community_id",$roominfo["community_id"])
								->find();
			$data["community_name"]=$communityinfo["community_name"];
			$res=Db::table("face_user_room")->insert($data);
			if($res)
			{
				return json(["errCode"=>"0",'error' => '房屋绑定成功']);
			}else
			{
				return json(["errCode"=>"1",'error' => '房屋绑定失败，请重新获取验证码绑定3']);
			}
		}
	}


	//短信验证码
    public function sendmsg($phone)
    {
        $code=rand(100000,999999);
        //$phone=request()->param("phone");
        $send = send_sms($phone, 1, ['code' => $code]);
        if($send['Message'] == 'OK') 
        {
            $info["phone"]=$phone;
            $info["code"]=$code;
            $info["ctime"]=time();
            $result=Db::table("face_sms_verify")->insert($info);
            if($result)
            {
            	return json(["errCode"=>"0",'error' => '验证码发送成功']);
                $arr=array("errCode"=>"0","msg"=>"验证码发送成功");
                return json_encode($arr);
                //die;
            }else
            {
            	return json(["errCode"=>"1",'error' => '请重新获取验证码']);
                $arr=array("errCode"=>"1","msg"=>"请重新获取验证码");
                return json_encode($arr);
                //die;
            }
        } else 
        {
        	return json(["errCode"=>"1",'error' => $send['Message']]);
            $arr=array("errCode"=>"1","msg"=>$send['Message']);
            return json_encode($arr);
            //die;
        }
    }

}